package com.example.ucenterservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.commonutils.JwtUtils;
import com.example.commonutils.MD5;
import com.example.servicebase.exceptionHandler.ExampleException;
import com.example.ucenterservice.entity.UcenterMember;
import com.example.ucenterservice.entity.vo.LoginVo;
import com.example.ucenterservice.entity.vo.RegisterVo;
import com.example.ucenterservice.mapper.UcenterMemberMapper;
import com.example.ucenterservice.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-03-28
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Override
    public String login(LoginVo loginVo) {
        String phone = loginVo.getPhone();
        String password = loginVo.getPassword();

        if(StringUtils.isBlank(password)||StringUtils.isBlank(phone)){
            throw new ExampleException(20001,"手机号或者电话号为空");
        }

        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",phone);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        if(ucenterMember==null){
            throw new ExampleException(20001,"手机号不存在");
        }

        if(!MD5.encrypt(password).equals(ucenterMember.getPassword())){
            throw new ExampleException(20001,"密码不正确");
        }

        if(ucenterMember.getIsDisabled()){
            throw new ExampleException(20001,"用户被禁用");
        }

        String token = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());

        return token;
    }

    @Override
    public void register(RegisterVo registerVo) {
        String nickname = registerVo.getNickname();
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();

        if(StringUtils.isBlank(nickname)||
                StringUtils.isBlank(code)||
                StringUtils.isBlank(mobile)||
                StringUtils.isBlank(password)){
            throw new ExampleException(20001,"传来的数据有空值，注册失败");
        }

        if(!code.equals("7777")){
            throw new ExampleException(20001,"验证码不正确，注册失败");
        }

        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count>=1){
            throw new ExampleException(20001,"手机号重复，注册失败");
        }

        UcenterMember member = new UcenterMember();
        member.setPassword(MD5.encrypt(password));//密码加密
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setIsDisabled(false);//用户不禁用
        member.setAvatar("https://online-teach-file.oss-cn-beijing.aliyuncs.com/teacher/2019/10/30/65423f14-49a9-4092-baf5-6d0ef9686a85.png");
        baseMapper.insert(member);

    }

    @Override
    public UcenterMember getMemberByOpenId(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }
}
